﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using SF.DBUtility;//Please add references
using SF.Common;

namespace SF.DAL
{
	/// <summary>
	/// 数据访问类:sf_modeconfig
	/// </summary>
	public partial class sf_modeconfig
	{
		public sf_modeconfig()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from sf_modeconfig");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        /// <summary>
        /// 查询用户名是否存在
        /// </summary>
        public bool Exists(string user_name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_modeconfig");
            strSql.Append(" where settingname=@settingname ");
            SqlParameter[] parameters = {
					new SqlParameter("@settingname", SqlDbType.NVarChar,100)};
            parameters[0].Value = user_name;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(SF.Model.sf_modeconfig model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into sf_modeconfig(");
			strSql.Append("settingNum,call_encoding,goodstype_onshelf,settingname,marketing_type,imgurl,onshelf,isdelete,createtime,createman,remark)");
			strSql.Append(" values (");
			strSql.Append("@settingNum,@call_encoding,@goodstype_onshelf,@settingname,@marketing_type,@imgurl,@onshelf,@isdelete,@createtime,@createman,@remark)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@settingNum", SqlDbType.NVarChar,50),
					new SqlParameter("@call_encoding", SqlDbType.NVarChar,50),
					new SqlParameter("@goodstype_onshelf", SqlDbType.NVarChar,200),
					new SqlParameter("@settingname", SqlDbType.NVarChar,100),
					new SqlParameter("@marketing_type", SqlDbType.Int,4),
					new SqlParameter("@imgurl", SqlDbType.NVarChar,300),
					new SqlParameter("@onshelf", SqlDbType.Int,4),
					new SqlParameter("@isdelete", SqlDbType.Int,4),
					new SqlParameter("@createtime", SqlDbType.DateTime),
					new SqlParameter("@createman", SqlDbType.NVarChar,50),
					new SqlParameter("@remark", SqlDbType.NVarChar,500)};
			parameters[0].Value = model.settingNum;
			parameters[1].Value = model.call_encoding;
			parameters[2].Value = model.goodstype_onshelf;
			parameters[3].Value = model.settingname;
			parameters[4].Value = model.marketing_type;
			parameters[5].Value = model.imgurl;
			parameters[6].Value = model.onshelf;
			parameters[7].Value = model.isdelete;
			parameters[8].Value = model.createtime;
			parameters[9].Value = model.createman;
			parameters[10].Value = model.remark;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SF.Model.sf_modeconfig model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update sf_modeconfig set ");
			strSql.Append("settingNum=@settingNum,");
			strSql.Append("call_encoding=@call_encoding,");
			strSql.Append("goodstype_onshelf=@goodstype_onshelf,");
			strSql.Append("settingname=@settingname,");
			strSql.Append("marketing_type=@marketing_type,");
			strSql.Append("imgurl=@imgurl,");
			strSql.Append("onshelf=@onshelf,");
			strSql.Append("isdelete=@isdelete,");
			strSql.Append("createtime=@createtime,");
			strSql.Append("createman=@createman,");
			strSql.Append("remark=@remark");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@settingNum", SqlDbType.NVarChar,50),
					new SqlParameter("@call_encoding", SqlDbType.NVarChar,50),
					new SqlParameter("@goodstype_onshelf", SqlDbType.NVarChar,200),
					new SqlParameter("@settingname", SqlDbType.NVarChar,100),
					new SqlParameter("@marketing_type", SqlDbType.Int,4),
					new SqlParameter("@imgurl", SqlDbType.NVarChar,300),
					new SqlParameter("@onshelf", SqlDbType.Int,4),
					new SqlParameter("@isdelete", SqlDbType.Int,4),
					new SqlParameter("@createtime", SqlDbType.DateTime),
					new SqlParameter("@createman", SqlDbType.NVarChar,50),
					new SqlParameter("@remark", SqlDbType.NVarChar,500),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = model.settingNum;
			parameters[1].Value = model.call_encoding;
			parameters[2].Value = model.goodstype_onshelf;
			parameters[3].Value = model.settingname;
			parameters[4].Value = model.marketing_type;
			parameters[5].Value = model.imgurl;
			parameters[6].Value = model.onshelf;
			parameters[7].Value = model.isdelete;
			parameters[8].Value = model.createtime;
			parameters[9].Value = model.createman;
			parameters[10].Value = model.remark;
			parameters[11].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sf_modeconfig ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sf_modeconfig ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_modeconfig GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,settingNum,call_encoding,goodstype_onshelf,settingname,marketing_type,imgurl,onshelf,isdelete,createtime,createman,remark from sf_modeconfig ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			SF.Model.sf_modeconfig model=new SF.Model.sf_modeconfig();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_modeconfig GetModel(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,settingNum,call_encoding,goodstype_onshelf,settingname,marketing_type,imgurl,onshelf,isdelete,createtime,createman,remark from sf_modeconfig ");
            if (where!="")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_modeconfig DataRowToModel(DataRow row)
		{
			SF.Model.sf_modeconfig model=new SF.Model.sf_modeconfig();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["settingNum"]!=null)
				{
					model.settingNum=row["settingNum"].ToString();
				}
				if(row["call_encoding"]!=null)
				{
					model.call_encoding=row["call_encoding"].ToString();
				}
				if(row["goodstype_onshelf"]!=null)
				{
					model.goodstype_onshelf=row["goodstype_onshelf"].ToString();
				}
				if(row["settingname"]!=null)
				{
					model.settingname=row["settingname"].ToString();
				}
				if(row["marketing_type"]!=null && row["marketing_type"].ToString()!="")
				{
					model.marketing_type=int.Parse(row["marketing_type"].ToString());
				}
				if(row["imgurl"]!=null)
				{
					model.imgurl=row["imgurl"].ToString();
				}
				if(row["onshelf"]!=null && row["onshelf"].ToString()!="")
				{
					model.onshelf=int.Parse(row["onshelf"].ToString());
				}
				if(row["isdelete"]!=null && row["isdelete"].ToString()!="")
				{
					model.isdelete=int.Parse(row["isdelete"].ToString());
				}
				if(row["createtime"]!=null && row["createtime"].ToString()!="")
				{
					model.createtime=DateTime.Parse(row["createtime"].ToString());
				}
				if(row["createman"]!=null)
				{
					model.createman=row["createman"].ToString();
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,settingNum,call_encoding,goodstype_onshelf,settingname,marketing_type,imgurl,onshelf,isdelete,createtime,createman,remark ");
			strSql.Append(" FROM sf_modeconfig ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,settingNum,call_encoding,goodstype_onshelf,settingname,marketing_type,imgurl,onshelf,isdelete,createtime,createman,remark ");
			strSql.Append(" FROM sf_modeconfig ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sf_modeconfig ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from sf_modeconfig T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM sf_modeconfig");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sf_modeconfig set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 获取ID
        /// 时间：2015-9-1
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public object GetID(string str)
        {
            string sql = @"select id from sf_modeconfig where settingNum = '{0}'";
            if (str.Trim() != "") 
            {
                sql = string.Format(sql, str);
            }
            return DbHelperSQL.GetSingle(sql.ToString());
        }

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "sf_modeconfig";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

